﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_LivestockMedication
    {
        public bool CreateOneLivestockMedication(LivestockMedication_Info objLivestockMedication_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockMedication_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HealthID", objLivestockMedication_Info.HealthID));
            command.Parameters.Add(new SqlParameter(@"VetID", objLivestockMedication_Info.VetID));
            command.Parameters.Add(new SqlParameter(@"DiseaseID", objLivestockMedication_Info.DiseaseID));
            command.Parameters.Add(new SqlParameter(@"InfectedDate", objLivestockMedication_Info.InfectedDate));
            command.Parameters.Add(new SqlParameter(@"Symptom", objLivestockMedication_Info.Symptom));
            command.Parameters.Add(new SqlParameter(@"TreatmentMethod", objLivestockMedication_Info.TreatmentMethod));
            command.Parameters.Add(new SqlParameter(@"TreatmentDate", objLivestockMedication_Info.TreatmentDate));
            command.Parameters.Add(new SqlParameter(@"TreatmentStatus", objLivestockMedication_Info.TreatmentStatus));
            command.Parameters.Add(new SqlParameter(@"QuarantineID", objLivestockMedication_Info.QuarantineID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneLivestockMedication(LivestockMedication_Info objLivestockMedication_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockMedication_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HealthID", objLivestockMedication_Info.HealthID));
            command.Parameters.Add(new SqlParameter("@VetID", objLivestockMedication_Info.VetID));
            command.Parameters.Add(new SqlParameter("@DiseaseID", objLivestockMedication_Info.DiseaseID));
            command.Parameters.Add(new SqlParameter("@InfectedDate", objLivestockMedication_Info.InfectedDate));
            command.Parameters.Add(new SqlParameter("@Symptom", objLivestockMedication_Info.Symptom));
            command.Parameters.Add(new SqlParameter("@TreatmentMethod", objLivestockMedication_Info.TreatmentMethod));
            command.Parameters.Add(new SqlParameter("@TreatmentDate", objLivestockMedication_Info.TreatmentDate));
            command.Parameters.Add(new SqlParameter("@TreatmentStatus", objLivestockMedication_Info.TreatmentStatus));
            command.Parameters.Add(new SqlParameter("@QuarantineID", objLivestockMedication_Info.QuarantineID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneLivestockMedication(int HealthID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockMedication_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HealthID", HealthID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllLivestockMedication()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockMedication_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public LivestockMedication_Info GetOneLivestockMedication(int HealthID)
        {
            LivestockMedication_Info objLivestockMedication_Info = new LivestockMedication_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockMedication_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HealthID", HealthID));
            try
            {
                objConnection.Open();
                SqlDataReader ordLivestockMedication = command.ExecuteReader(); //OjectRecordDatareader
                ordLivestockMedication.Read();
                if (ordLivestockMedication.HasRows)
                {
                    if (ordLivestockMedication["HealthID"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.HealthID = Convert.ToInt16(ordLivestockMedication["HealthID"]);
                    }
                    if (ordLivestockMedication["VetID"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.VetID = Convert.ToInt16(ordLivestockMedication["VetID"]);
                    }
                    if (ordLivestockMedication["DiseaseID"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.DiseaseID = Convert.ToInt16(ordLivestockMedication["DiseaseID"]);
                    }
                    if (ordLivestockMedication["InfectedDate"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.InfectedDate = Convert.ToDateTime(ordLivestockMedication["InfectedDate"]);
                    }
                    if (ordLivestockMedication["Symptom"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.Symptom = Convert.ToString(ordLivestockMedication["Symptom"]);
                    }
                    if (ordLivestockMedication["TreatmentMethod"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.TreatmentMethod = Convert.ToString(ordLivestockMedication["TreatmentMethod"]);
                    }
                    if (ordLivestockMedication["TreatmentDate"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.TreatmentDate = Convert.ToDateTime(ordLivestockMedication["TreatmentDate"]);
                    }
                    if (ordLivestockMedication["TreatmentStatus"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.TreatmentStatus = Convert.ToBoolean(ordLivestockMedication["TreatmentStatus"]);
                    }
                    if (ordLivestockMedication["QuarantineID"] != DBNull.Value)
                    {
                        objLivestockMedication_Info.QuarantineID = Convert.ToInt16(ordLivestockMedication["QuarantineID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objLivestockMedication_Info;
        }
    }
}
